package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.domain.Deploy;
import com.dss.auto_deploy.dto.SelectDto;
import com.dss.auto_deploy.event.StartToDeployEvent;
import com.dss.auto_deploy.service.IDeployService;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.vo.DeployVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: dongshoushan
 * @date: 2021年01月18日 16:42
 */
@RestController
@RequestMapping("/deploy")
@Slf4j
public class DeployController {
    @Resource
    private IDeployService deployService;
    @Resource
    private ApplicationEventPublisher publisher;
    /**
     * 查询所有配置
     *
     * @return
     */
    @GetMapping("/list")
    public Result list(SelectDto pageReq) {
        return Result.success(deployService.getList(pageReq));
    }

    /**
     * 新增自动部署
     *
     * @param deploy
     * @return
     */
    @PostMapping("/add")
    public Result insertDeploy(@RequestBody Deploy deploy) {
        deployService.insertDeploy(deploy);
        return Result.success("新增自动部署成功");
    }

    /**
     * 修改自动部署
     *
     * @param deploy
     * @return
     */
    @PostMapping("/updateDeploy")
    public Result updateDeploy(@RequestBody Deploy deploy) {
        deployService.updateDeploy(deploy);
        return Result.success("修改自动部署成功");
    }

    /**
     * 删除自动部署
     *
     * @param id
     * @return
     */
    @PostMapping("/deleteDeploy")
    public Result deleteDeploy(Long id) {
        deployService.deleteDeploy(id);
        return Result.success("删除自动部署成功");
    }
    /**
     * 查询当前部署成功的数据
     *
     * @param
     * @return
     */
    @GetMapping("/getSuccess")
    public Result<List<DeployVo>> getSuccess() {
        List<DeployVo> list = deployService.getSuccess();
        return Result.success(list);
    }
    /**
     * 根据id查询
     *
     * @param
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<Deploy> getById(@PathVariable Integer id) {
        return Result.success(deployService.getById(id));
    }

    /**
     * 自动批量部署代码（后端）
     *
     * @param ids
     * @return
     */
    @PostMapping("/deploySelect")
    public Result deploySelect(String ids) {
        publisher.publishEvent(new StartToDeployEvent(ids));
        return Result.success("请求成功");
    }
    /**
     * 重启服务（后端）
     *
     * @param ids
     * @return
     */
    @PostMapping("/restartService")
    public Result restartService(String ids) {
        try {
            Boolean select = deployService.restartService(ids);
            if (select) {
                return Result.success("已自动重启服务");
            } else {
                return Result.fail("自动重启服务失败,请检查命令配置");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 停止服务（后端）
     *
     * @param ids
     * @return
     */
    @PostMapping("/stopService")
    public Result stopService(String ids) {
        try {
            Boolean select = deployService.stopService(ids);
            if (select) {
                return Result.success("已停止服务");
            } else {
                return Result.fail("停止服务失败,请检查命令配置");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }
    }
    /**
     * 自动批量部署代码（前端）
     *
     * @return
     */
    @PostMapping("/javaScriptDeployBatch")
    public Result javaScriptDeployBatch(String ids) {
        try {
            List<Long> split = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
            Boolean select = deployService.javaScriptDeployBatch(split);
            if (select) {
                return Result.success("批量部署代码成功");
            } else {
                return Result.fail("自动部署代码失败,请检查命令配置");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return Result.fail(e.getMessage());
        }

    }
}
